package com.masterclient.app.modules.aftersale.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.aftersale.AfterSaleDetailData;
import com.eoner.baselibrary.bean.aftersale.AfterSalePurchaseBtnData;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @auther hjian
 * @date 2020/9/29.
 */
public class AfterSalePurchaseDetailPresenter extends AfterSalePurchaseDetailContract.Presenter{

    @Override
    public void afterSaleDetail(String order_id, String order_product_id) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id",order_id);
            jsonObject.put("order_product_id",order_product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().afterSalePurchaseDetail(body), new Consumer<CommonBaseResponse<AfterSaleDetailData>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<AfterSaleDetailData> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showResult(0, true);
                    mView.showAfterSaleInfo(bean.getData());
                }else {
                    mView.showResult(0, false);
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showResult(0, false);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void closeAfterSale(String refund_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("refund_id",refund_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().afterSalePurchaseCancel(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showResult(1, true);
                    ToastUtil.showToast("撤销申请成功");
                }else {
                    mView.showResult(1, false);
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                mView.showResult(1, false);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void getRefundBtn(int type, String refund_id, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("正在加载...");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("refund_id",refund_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().afterSalePurchaseBtn(body), new Consumer<CommonBaseResponse<AfterSalePurchaseBtnData>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<AfterSalePurchaseBtnData> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showRefundBtn(type, bean.getData());
                }else {
                    progressLoginDialog.dismiss();
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }
}
